package com.qq.BFMRSE.extractWord;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.DocsEnum;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.Terms;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.BytesRef;

import com.qq.BFMRSE.entity.PostStopAndLowCaseAnalyzer;
import com.qq.BFMRSE.entity.tfWordInfo;

public class luIndexUtil {
	public static boolean deleteLuIndex(String luIndexPath,String fileName){
		boolean ret=false;
		Analyzer analyzer = new PostStopAndLowCaseAnalyzer();
		IndexWriterConfig iwc=new IndexWriterConfig(analyzer);
		Path indexPath=FileSystems.getDefault().getPath(luIndexPath);
		IndexWriter iWriter=null;
		Directory directory=null;
		try {
			directory=FSDirectory.open(indexPath);
			iWriter=new IndexWriter(directory,iwc);
			iWriter.deleteDocuments(new Term("path", fileName));
			iWriter.commit();
			ret=true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			if(iWriter!=null)
			{
				try {
					iWriter.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return ret;
	}
	/**
	 * 撤销删除操作
	 * @param luIndex
	 */
	public static void unDelete(String luIndex){
		 IndexWriter indexWriter = null;
	        try {
	            Directory directory = FSDirectory.open(FileSystems.getDefault().getPath(luIndex));
	            Analyzer analyzer = new StandardAnalyzer();
	            IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
	            indexWriter = new IndexWriter(directory, indexWriterConfig);
	            indexWriter.rollback();
	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally {
	            try {
	                if (indexWriter != null) {
	                    indexWriter.close();
	                }
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	        }
	    }
	/**
	 * 强制删除
	 */
	public static void forceDelete() {  
	    IndexWriter indexWriter = null;  
	    try {  
	        Directory directory = FSDirectory.open(FileSystems.getDefault().getPath("F:/test/lucene/index"));  
	        Analyzer analyzer = new StandardAnalyzer();  
	        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);  
	        indexWriter = new IndexWriter(directory, indexWriterConfig);  
	        indexWriter.forceMergeDeletes();  
	    } catch (Exception e) {  
	        e.printStackTrace();  
	    } finally {  
	        try {  
	            if (indexWriter != null) {  
	                indexWriter.close();  
	            }  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
	    }  
	}
	public static void update(String luIndex,String filePath){
		IndexWriter indexWriter = null;
        try {
            Directory directory = FSDirectory.open(FileSystems.getDefault().getPath(luIndex));
            Analyzer analyzer = new StandardAnalyzer();
            IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
            indexWriter = new IndexWriter(directory, indexWriterConfig);
            /**
             * Lucene并没有提供更新，这里的更新操作其实是如下两个操作的合集 先删除之后再添加
             */
            File newFile=new File(filePath);
            String temp=SelectHelper.getText(newFile.getCanonicalPath());
			String fileName=SelectHelper.getReFileName(newFile.getPath());
			Document document=new Document();
			//document.add(new Field("path", fileName, Field.Store.YES, Field.Index.ANALYZED));
			//document.add(new Field("body", temp,Field.Store.YES, Field.Index.ANALYZED));
			Field fieldPath=new Field("path", SelectHelper.getReFileName(newFile.getPath()),Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
			Field fieldBody=new Field("body",temp,Field.Store.YES,Field.Index.ANALYZED,    
                    Field.TermVector.WITH_POSITIONS_OFFSETS); 
			document.add(fieldPath);    
            document.add(fieldBody);    
            indexWriter.addDocument(document);
           // indexWriter.updateDocument(new Term("id", "1"), document);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (indexWriter != null) {
                    indexWriter.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
	public static void check(String luIndex) {  
	    IndexReader indexReader = null;  
	    try {  
	    	Path indexPath=FileSystems.getDefault().getPath(luIndex);
	    	indexReader = DirectoryReader.open(FSDirectory.open(indexPath));
	        // 通过reader可以有效的获取到文档的数量  
	        // 有效的索引文档  
	        System.out.println("有效的索引文档:" + indexReader.numDocs());  
	        // 总共的索引文档  
	        System.out.println("总共的索引文档:" + indexReader.maxDoc());  
	        // 删掉的索引文档，其实不恰当，应该是在回收站里的索引文档  
	        System.out.println("删掉的索引文档:" + indexReader.numDeletedDocs());  
	    } catch (Exception e) {  
	        e.printStackTrace();  
	    } finally {  
	        try {  
	            if (indexReader != null) {  
	                indexReader.close();  
	            }  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
	    }  
	}
}
